package com.gmzg.gmzgoffice.works;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;

import com.gmzg.gmzgoffice.R;
import com.gmzg.gmzgoffice.works.adapter.ViewPagerAdapter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ApproveActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.tablayout)
    TabLayout mTablayout;
    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.new_approve_btn)
    Button mNewApproveBtn;
    private AlertDialog mDialog;
    private int mPosition;
    private SetTabPositionListener mListener;
    interface SetTabPositionListener{
        void setTabPosition(int position);
    }

    public void setListener(SetTabPositionListener listener) {
        mListener = listener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_approve);
        ButterKnife.bind(this);
        mToolbarTitle.setText("审批");
        setSupportActionBar(mToolbar);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowTitleEnabled(false);
        }
        List<ApproveFragment> fragments = new ArrayList<>();
        for (int i = 0; i < 3; i++) {
            ApproveFragment approveFragment = new ApproveFragment();
            Bundle bundle = new Bundle();
            bundle.putInt(Constant.CATEGORY,i);
            approveFragment.setArguments(bundle);
            fragments.add(approveFragment);
        }
        mViewpager.setAdapter(new ViewPagerAdapter(getSupportFragmentManager(), fragments));
        mViewpager.addOnPageChangeListener(this);
        //获取ViewPager当前所指的位置
        mTablayout.setupWithViewPager(mViewpager);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @OnClick(R.id.new_approve_btn)
    public void onClick() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("发起审批");
        String[] strings = {"请假", "报销", "出差"};
        builder.setSingleChoiceItems(strings, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Intent intent = null;
                switch (which){
                    case 0:
                        intent = new Intent(ApproveActivity.this,VacateActivity.class);
                        break;
                    case 1:
                        intent = new Intent(ApproveActivity.this,PaymentActivity.class);
                        break;
                    case 2:
                        intent = new Intent(ApproveActivity.this,BusinessActivity.class);
                        break;
                }
                if (intent != null) {
                    startActivity(intent);
                }
            }
        });
        mDialog = builder.create();
        mDialog.show();
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mDialog != null) {
            mDialog.hide();
        }
    }

//    //viewPager的滑动事件
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (mListener != null) {
            mListener.setTabPosition(position);
        }
//        EventBus.getDefault().post(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
